﻿using GKQC.Common;
using GKQC.DbHelper.Dao;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.Services;
using GKQC.Language;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace GKQC
{
    public partial class FormLock : Form
    {
        UserModel user = null;
        AllDbContext dbContext = null;
        List<LockModel> sysParams = null;
        FormStatus formStatus = FormStatus.Browser;
        readonly string countText = "記錄數：{0}";

        public FormLock(UserModel user, AllDbContext dbContext)
        {
            InitializeComponent();

            try
            {
                this.user = user;
                this.dbContext = dbContext;

                dataGridView1.RowPostPaint += DataGridView1_RowPostPaint;
                dataGridView1.AutoGenerateColumns = false;
                BindData();
                LanguageHelper.SetControlLanguageText(this);
            }
            catch(Exception ex)
            {
                QMessageBox.Show(ex.Message);
                Logger.LogError(ex);
            }
        }

        private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewSet.RowPostPaint(sender, e);
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {

        }

        private void tsmCancel_Click(object sender, EventArgs e)
        {

        }


        private void tsmSave_Click(object sender, EventArgs e)
        {
            try
            {
               
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message,"異常S0002", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void tsmDelete_Click(object sender, EventArgs e)
        {
            int i = 0;
            List<LockModel> delList = new List<LockModel>();
            foreach (DataGridViewRow r in dataGridView1.SelectedRows)
            {
                LockModel model = r.DataBoundItem as LockModel;
                if (model != null) delList.Add(model);
                
            }
            if(delList.Count == 0)
            {
                if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.DataBoundItem != null)
                {
                    LockModel lockModel = dataGridView1.CurrentRow.DataBoundItem as LockModel;
                    if (lockModel != null)
                        delList.Add(lockModel);
                }
            }
            if (delList.Count == 0)
            {
                QMessageBox.Show("請選擇要刪除的行", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                return;
            }
            else
            {
                if (QMessageBox.Show("數據刪除後無法恢復，請確認是否刪除", "請確認",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)
                    == DialogResult.Cancel)
                    return;
            }

            if (delList.Count > 0)
                i = dbContext.QcDb.Deleteable(delList).ExecuteCommand();

            if (i>0)
            {
                QMessageBox.Show("成功刪除記錄 " + i.ToString() + " 條。", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                BindData();
            }
            else
            {
                QMessageBox.Show("未刪除任何記錄","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }

        private void tsmExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {

        }

        void BindData()
        {
            this.sysParams = this.dbContext.QcDb.Queryable<LockModel>().Take(3000).ToList();
            dataGridView1.DataSource = new BindingList<LockModel>(sysParams);
            toolStripStatusLabel1.Text = string.Format(countText, this.sysParams.Count.ToString());
        }


        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
        }

        private void tsmQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
